home *** CD-ROM | disk | FTP | other *** search
- #
- # THIS IS WORK IN PROGRESS
- #
- # The Python Imaging Library
- # $Id: BdfFontFile.py,v 1.1.1.2 1999/01/13 09:40:15 sjoerd Exp $
- #
- # bitmap distribution font file parser
- #
- # history:
- # 96-05-16 fl created (as bdf2pil)
- # 97-08-25 fl converted to FontFile driver
- #
- # Copyright (c) Secret Labs AB 1997-98.
- # Copyright (c) Fredrik Lundh 1997.
- #
- # See the README file for information on usage and redistribution.
- #
-
- import Image
- import FontFile
-
- import string
-
- # --------------------------------------------------------------------
- # parse X Bitmap Distribution Format (BDF)
- # --------------------------------------------------------------------
-
- bdf_slant = {
- "R": "Roman",
- "I": "Italic",
- "O": "Oblique",
- "RI": "Reverse Italic",
- "RO": "Reverse Oblique",
- "OT": "Other"
- }
-
- bdf_spacing = {
- "P": "Proportional",
- "M": "Monospaced",
- "C": "Cell"
- }
-
- def bdf_char(f):
-
- # skip to STARTCHAR
- while 1:
- s = f.readline()
- if not s:
- return None
- if s[:9] == "STARTCHAR":
- break
- id = string.strip(s[9:])
-
- # load symbol properties
- props = {}
- while 1:
- s = f.readline()
- if not s or s[:6] == "BITMAP":
- break
- i = string.find(s, " ")
- props[s[:i]] = s[i+1:-1]
-
- # load bitmap
- bitmap = []
- while 1:
- s = f.readline()
- if not s or s[:7] == "ENDCHAR":
- break
- bitmap.append(s[:-1])
- bitmap = string.join(bitmap, "")
-
- [x, y, l, d] = map(string.atoi, string.split(props["BBX"]))
- [dx, dy] = map(string.atoi, string.split(props["DWIDTH"]))
-
- bbox = (dx, dy), (l, -d-y, x+l, -d), (0, 0, x, y)
-
- im = Image.fromstring("1", (x, y), bitmap, "hex", "1")
-
- return id, string.atoi(props["ENCODING"]), bbox, im
-
-
- class BdfFontFile(FontFile.FontFile):
-
- def __init__(self, fp):
-
- s = fp.readline()
- if s[:13] != "STARTFONT 2.1":
- raise SyntaxError, "not a valid BDF file"
-
- FontFile.FontFile.__init__(self)
-
- props = {}
- comments = []
-
- while 1:
- s = fp.readline()
- if not s or s[:13] == "ENDPROPERTIES":
- break
- i = string.find(s, " ")
- props[s[:i]] = s[i+1:-1]
- if s[:i] in ["COMMENT", "COPYRIGHT"]:
- if string.find(s, "LogicalFontDescription") < 0:
- comments.append(s[i+1:-1])
-
- font = string.split(props["FONT"], "-")
-
- font[4] = bdf_slant[font[4]]
- font[11] = bdf_spacing[font[11]]
-
- ascent = string.atoi(props["FONT_ASCENT"])
- descent = string.atoi(props["FONT_DESCENT"])
-
- fontname = string.join(font[1:], ";")
-
- # print "#", fontname
- # for i in comments:
- # print "#", i
-
- font = []
- while 1:
- c = bdf_char(fp)
- if not c:
- break
- id, ch, (xy, dst, src), im = c
- if ch >= 0:
- self.glyph[ch] = xy, dst, src, im
-